package com.example.pc.videodemo

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.telecom.Call
import com.example.myapplication.CrashHandler
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.*
import javax.net.ssl.*

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        val crashHandler: CrashHandler? = CrashHandler.instance
        crashHandler?.init(getApplicationContext())
        handler = Handler(Looper.getMainLooper())
        context = this@MyApplication

//        ThreadPool.instance.execute(object : Runnable {
//            public override fun run() {
//                MqttManager.instance
//                try {
//                    InitialAppEngine.initial(context)
//                    MusicManager.Companion.getInstance()
//                } catch (e: Exception) {
//                    e.printStackTrace()
//                }
//            }
//        })
//        HttpEngine.Companion.createInstance()
        handleSSLHandshake()
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            public override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                if (activity is MainActivity) {
                    for (activity1: Activity? in stack) {
                        activity1?.finish()
                    }
                    stack.clear()
                }
                stack.add(activity)
            }

            public override fun onActivityStarted(activity: Activity) {}
            public override fun onActivityResumed(activity: Activity) {
                if (activity is MainActivity) {
                    for (activity1: Activity? in stack) {
                        if (!(activity1 is MainActivity)) activity1?.finish()
                    }
                    stack.clear()
                }
                stack.add(activity)
            }

            public override fun onActivityPaused(activity: Activity) {}
            public override fun onActivityStopped(activity: Activity) {}
            public override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            public override fun onActivityDestroyed(activity: Activity) {
                if (activity is MainActivity) {
                    for (activity1: Activity? in stack) {
                        activity1?.finish()
                    }
                    System.exit(0)
                }
            }
        })
    }

    companion object {
        private val stack: Stack<Activity?> = Stack()
        var handler: Handler? = null
        var context: Context? = null
        fun getStack(): Stack<Activity?> {
            return stack;
        }

        val appContext: Context?
            get() {
                return context
            }

        fun handleSSLHandshake() {
            try {
                val trustAllCerts: Array<TrustManager> = arrayOf(object : X509TrustManager {
                    public override fun getAcceptedIssuers(): Array<X509Certificate?> {
                        return arrayOfNulls(0)
                    }

                    public override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) {}
                    public override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) {}
                })
                val sc: SSLContext = SSLContext.getInstance("TLS")
                // trustAllCerts信任所有的证书
                sc.init(null, trustAllCerts, SecureRandom())
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
                HttpsURLConnection.setDefaultHostnameVerifier(object : HostnameVerifier {
                    public override fun verify(hostname: String, session: SSLSession): Boolean {
                        return true
                    }
                })
            } catch (ignored: Exception) {
            }
        }

        private var call: Call? = null
        fun setCall(call: Call) {
            this.call = call

        }
        fun getCall():Call?{
            return call;
        }
    }
}
